<template>
    <div>
      <!--面包屑导航-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>

      <el-card>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addGoods">添加商品</el-button>
          </el-col>
        </el-row>
      </el-card>

      <!--表格数据-->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="序号" width="50px"></el-table-column>
        <el-table-column label="商品名称" prop="goodsName"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goodsPrice" width="110px"></el-table-column>
        <el-table-column label="商品重量" prop="goodsWeight" width="80px"></el-table-column>
        <el-table-column label="创建时间" width="170px">
          <template v-slot="scope">
            {{scope.row.addTime | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editGoods(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row.goodsId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageIndex"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </div>
</template>

<script>
    import Vue from "vue";

    export default {
        name: "list",
      data(){
          return{
            queryInfo:{
              query:'',
              pageIndex:1,
              pageSize:5
            },
            goodsList:[],
            total:0
          }
      },
      created() {
          this.getGoodsList();
      },
      methods:{
        async getGoodsList(){
          const {data:res} = await this.$axios.get("goods",{params:this.queryInfo});
          if(res.code != 200) {
            return this.$message.error("获取商品列表失败");
          }
          this.$message.success("获取商品列表成功");
          console.log(res.data.goods);
          this.goodsList = res.data.goods;
          this.total = res.data.total;
        },
        //触发当前最新的pageSize方法
        handleSizeChange(val){
          this.queryInfo.pageSize = val;
          this.getGoodsList();
        },
        handleCurrentChange(val){
          this.queryInfo.pageIndex = val;
          this.getGoodsList();
        },
        //删除商品事件
        async removeGoods(id){
          const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).catch(err=>err);
          if(confirmResult != 'confirm') {
            return this.$message.info("已取消操作");
          }
          const {data:res} = await this.$axios.delete("delete/goods",{params:{goodsId:id}});
          if(res.code != 200) {
            return this.$message.error("删除商品失败");
          }
          this.$message.success("删除商品成功");
          this.getGoodsList();
        },
        //监听添加按钮路由页面
        addGoods() {
          this.$router.push("/goods/add");
        },
        editGoods(row) {
          this.$router.push({path:'/goods/edit', query:row});
        }
      },
      //自定义过滤器
      filters:{
        dateFormat(originVal){
          const dt = new Date(originVal);
          const y = dt.getFullYear();
          const m = (dt.getMonth() +1 + '').padStart(2,'0');
          const d = (dt.getDay() + '').padStart(2,'0');
          const hh = (dt.getHours() + '').padStart(2,'0');
          const mm = (dt.getMinutes() + '').padStart(2,'0');
          const ss = (dt.getSeconds() + '').padStart(2,'0');
          //return y;
          return y+'-'+m+'-'+d+' '+hh+':'+mm+':'+ss;
        }
      }
    }
</script>

<style scoped>
.el-card{
  margin-top: 15px;
}
</style>
